//
//  UIButton+Category.m
//  ChangGengHospital
//
//  Created by 章松 on 16/11/3.
//  Copyright © 2016年 zhangsong. All rights reserved.
//

#import "UIButton+Category.h"

@implementation UIButton (Category)
+(UIButton *)creatButtonWithFrame:(CGRect)frame
                  onview:(UIView *)superview
             block:(void(^)(UIButton *button))buttonBlock
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    if (superview) {
        [superview addSubview:button];
    }
    [button addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        if (buttonBlock) {
            buttonBlock(sender);
        }
    }];
    
    return button;
}

+(UIButton *)creatButtonWithFrame:(CGRect)frame
               setBackgroundImage:(NSString *)imageViewName
                     onview:(UIView *)superview
                block:(void(^)(UIButton *button))buttonBlock
{
    UIButton *button = [self creatButtonWithFrame:frame onview:superview block:^(UIButton *button) {
        buttonBlock(button);
    }];
    [button setBackgroundImage:[UIImage imageNamed:imageViewName] forState:UIControlStateNormal];
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    return button;
}

+(UIButton *)creatButtonWithFrame:(CGRect)frame
                        setImage:(NSString *)imageViewName
                           onview:(UIView *)superview
                            block:(void(^)(UIButton *button))buttonBlock
{
    UIButton *button = [self creatButtonWithFrame:frame onview:superview block:^(UIButton *button) {
        buttonBlock(button);
    }];
    [button setImage:[UIImage imageNamed:imageViewName] forState:UIControlStateNormal];
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    return button;
}
+(UIButton *)creatButtonWithFrame:(CGRect)frame
                            title:(NSString *)title
                        titleColor:(UIColor *)titleColor
                             font:(UIFont *)font
                           onview:(UIView *)superview
                        block:(void(^)(UIButton *button))buttonBlock
{
    UIButton *button = [self creatButtonWithFrame:frame onview:superview block:^(UIButton *button) {
        buttonBlock(button);
    }];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = font;
    
    return button;
}

/**
 * @param color 按钮字体颜色
 * @param font 按钮字体
 */
+ (UIButton *)createBtnTitleColor:(UIColor *)color
                             font:(UIFont *)font {
    return [self createBtnTitleColor:color font:font title:nil];
}
+ (UIButton *)createBtnTitleColor:(UIColor *)color
                             font:(UIFont *)font
                            title:(NSString *)title {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    [btn setTitle:title forState:UIControlStateNormal];
    return btn;
}
 
#pragma mark - 图文间距
- (void)setIconInLeftWithSpacing:(CGFloat)Spacing
{
    self.titleLabel.lineBreakMode = NSLineBreakByClipping;
    self.titleEdgeInsets = (UIEdgeInsets){
        .top    = 0,
        .left   = Spacing/2,
        .bottom = 0,
        .right  = -Spacing/2,
    };
    
    self.imageEdgeInsets = (UIEdgeInsets){
        .top    = 0,
        .left   = -Spacing/2,
        .bottom = 0,
        .right  = Spacing/2,
    };
}

- (void)setIconInRightWithSpacing:(CGFloat)Spacing
{
    self.titleLabel.lineBreakMode = NSLineBreakByClipping;
    CGFloat img_W = self.imageView.frame.size.width;
    CGFloat tit_W = self.titleLabel.frame.size.width;
    
    self.titleEdgeInsets = (UIEdgeInsets){
        .top    = 0,
        .left   = - (img_W + Spacing / 2),
        .bottom = 0,
        .right  =   (img_W + Spacing / 2),
    };
    
    self.imageEdgeInsets = (UIEdgeInsets){
        .top    = 0,
        .left   =   (tit_W + Spacing / 2),
        .bottom = 0,
        .right  = - (tit_W + Spacing / 2),
    };
}
- (void)setIconInTopWithSpacing:(CGFloat)Spacing
{
    self.titleLabel.lineBreakMode = NSLineBreakByClipping;
    CGFloat img_W = self.imageView.frame.size.width;
    CGFloat img_H = self.imageView.frame.size.height;
    CGFloat tit_W = self.titleLabel.intrinsicContentSize.width;
    CGFloat tit_H =self.titleLabel.intrinsicContentSize.height;

    self.titleEdgeInsets = (UIEdgeInsets){
        .top    =   0,
        .left   = - img_W ,
        .bottom = - (img_H + Spacing / 2),
        .right  =   0,
    };

    self.imageEdgeInsets = (UIEdgeInsets){
        .top    = - (tit_H / 2 + Spacing / 2),
        .left   =   0,
        .bottom =   0,
        .right  = - (tit_W),
    };
}
- (void)setIconInBottomWithSpacing:(CGFloat)Spacing
{
    self.titleLabel.lineBreakMode = NSLineBreakByClipping;
    CGFloat img_W = self.imageView.frame.size.width;
    CGFloat img_H = self.imageView.frame.size.height;
    CGFloat tit_W = self.titleLabel.frame.size.width;
    CGFloat tit_H = self.titleLabel.frame.size.height;
    
    self.titleEdgeInsets = (UIEdgeInsets){
        .top    = - (tit_H / 2 + Spacing / 2),
        .left   = - (img_W / 2),
        .bottom =   (tit_H / 2 + Spacing / 2),
        .right  =   (img_W / 2),
    };
    
    self.imageEdgeInsets = (UIEdgeInsets){
        .top    =   (img_H / 2 + Spacing / 2),
        .left   =   (tit_W / 2),
        .bottom = - (img_H / 2 + Spacing / 2),
        .right  = - (tit_W / 2),
    };
}
@end
